package com.testmycat.zbf.chongshi;

import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryListener;
import org.springframework.retry.interceptor.RetryOperationsInterceptor;
import org.springframework.stereotype.Component;

/**
 * @author: LCG
 * @date: 2022-05-16 20:45:20
 * @description:
 **/
@Component("mySpringRetryListener")
public class MySpringRetryListener implements RetryListener {
    @Override
    public <T, E extends Throwable> boolean open(RetryContext retryContext, RetryCallback<T, E> retryCallback) {

        System.out.println("====open====");

        return true;
    }

    @Override
    public <T, E extends Throwable> void close(RetryContext retryContext, RetryCallback<T, E> retryCallback, Throwable throwable) {
        System.out.println("====close====");
    }

    @Override
    public <T, E extends Throwable> void onError(RetryContext retryContext,
                                                 RetryCallback<T, E> retryCallback, Throwable throwable) {
        String message = retryContext.getLastThrowable().getMessage();

        RetryOperationsInterceptor retryOperationsInterceptor=(RetryOperationsInterceptor)retryCallback;


        System.out.println("===================pppp====>"+message);
        retryContext.getRetryCount();//获取是第几次尝试

        System.out.println("====onError====");
        try {
            retryCallback.doWithRetry(retryContext);
        } catch (Throwable e) {
            e.printStackTrace();
        }


    }
}
